home *** CD-ROM | disk | FTP | other *** search
- function CPuck()
- {
- }
- function VectorSize(vec)
- {
- return Math.sqrt(vec.x * vec.x + vec.y * vec.y);
- }
- CPuck.prototype = new MovieClip();
- Object.registerClass("CPuck",CPuck);
- CPuck.prototype.GoIdle = function(really)
- {
- this._idle = really;
- };
- CPuck.prototype.onEnterFrame = function()
- {
- var motionSegment = new Object();
- var timeWithinFrame = 0;
- if(!this._velX || !this._velY)
- {
- return undefined;
- }
- while(timeWithinFrame < 1)
- {
- motionSegment.origin = new Object();
- motionSegment.origin.x = this._x;
- motionSegment.origin.y = this._y;
- motionSegment.direction = new Object();
- motionSegment.direction.x = this._velX;
- motionSegment.direction.y = this._velY;
- var collisionInfo = this.HitPaddle(motionSegment);
- if(collisionInfo == null)
- {
- collisionInfo = this.HitHump(motionSegment);
- if(collisionInfo != null)
- {
- this._parent.BallHitHump();
- }
- }
- else
- {
- this._parent.BallHit();
- }
- if(collisionInfo != null)
- {
- timeWithinFrame += (1 - timeWithinFrame) * collisionInfo.touchTime;
- var mirroredVel = MirrorVector({x:this._velX,y:this._velY},collisionInfo.touchAngle / 180 * 3.141592653589793);
- this._velX = - mirroredVel.x;
- this._velY = - mirroredVel.y;
- motionSegment.origin = collisionInfo.touchPoint;
- motionSegment.direction.x = this._velX * (1 - timeWithinFrame);
- motionSegment.direction.y = this._velY * (1 - timeWithinFrame);
- }
- else
- {
- timeWithinFrame = 1;
- this._x = motionSegment.origin.x + motionSegment.direction.x;
- this._y = motionSegment.origin.y + motionSegment.direction.y;
- }
- }
- if(this.IsOutOfBounds())
- {
- this._parent.PuckOutOfBounds();
- }
- };
- CPuck.prototype.HitHump = function(motionSegment)
- {
- var humpCircle = new Object();
- humpCircle.center = new Object();
- humpCircle.center.x = this._parent._hump._x;
- humpCircle.center.y = this._parent._hump._y;
- humpCircle.radius = this._parent._hump._width * 0.5 + 4;
- var firstIntersectionInfo = FindSegmentCircleIntersections(motionSegment,humpCircle);
- if(firstIntersectionInfo)
- {
- var collisionInfo = new Object();
- collisionInfo.touchPoint = firstIntersectionInfo.touchPoint;
- var deltaX = humpCircle.center.x - collisionInfo.touchPoint.x;
- var deltaY = humpCircle.center.y - collisionInfo.touchPoint.y;
- collisionInfo.touchAngle = Math.atan2(deltaY,deltaX) / 3.141592653589793 * 180;
- collisionInfo.touchTime = firstIntersectionInfo.touchTime;
- return collisionInfo;
- }
- return null;
- };
- CPuck.prototype.HitPaddle = function(motionSegment)
- {
- var earliestCollisionInfo = null;
- var destination = new Object();
- destination.x = motionSegment.origin.x + motionSegment.direction.x;
- destination.y = motionSegment.origin.y + motionSegment.direction.y;
- if(!this._idle && VectorSize(destination) >= 124)
- {
- iPaddle = 1;
- while(iPaddle <= this._parent._nPaddles)
- {
- var paddle = this._parent["paddle" + iPaddle];
- var arc = new Object();
- arc.center = new Object();
- arc.center.x = 0;
- arc.center.y = 0;
- arc.radius = 124;
- arc.angularMidpoint = paddle._rotation;
- arc.arcLength = paddle._arcLength;
- var containsOrigin = ArcContainsPoint(arc,motionSegment.origin);
- var containsDest = ArcContainsPoint(arc,destination);
- if(!(!containsOrigin && !containsDest))
- {
- var firstIntersectionInfo = FindSegmentArcIntersections(motionSegment,arc);
- if(firstIntersectionInfo)
- {
- var collisionInfo = new Object();
- collisionInfo.touchAngle = WrapAngle(paddle._rotation);
- collisionInfo.touchPoint = firstIntersectionInfo.touchPoint;
- collisionInfo.touchTime = firstIntersectionInfo.touchTime;
- if(!earliestCollisionInfo || collisionInfo.touchTime < earliestCollisionInfo.touchTime)
- {
- earliestCollisionInfo = collisionInfo;
- }
- }
- }
- iPaddle++;
- }
- }
- return earliestCollisionInfo;
- };
- CPuck.prototype.MoveInsideEdge = function()
- {
- while(this.DistanceFromCenter() > 118)
- {
- this._x -= this._velX;
- this._y -= this._velY;
- }
- };
- CPuck.prototype.DistanceFromCenter = function()
- {
- return Math.sqrt(this._x * this._x + this._y * this._y);
- };
- CPuck.prototype.IsOutOfBounds = function()
- {
- return !this._idle ? this.DistanceFromCenter() > 142 : false;
- };
-